<?php

namespace app\controller\api;

use app\base\BaseEncryptController;
use app\factory\LoginUser;
use app\factory\User as UserFacade;
use app\libs\AliyunSms;
use app\libs\Util;
use app\model\business\BusinessModel;
use app\model\order\OrderModel;
use app\model\store\StoreDepositModel;
use app\model\store\StoreModel;
use app\model\system\LogisticsModel;
use app\model\system\SysConfigModel;
use app\model\user\UserLevelModel;
use app\model\user\UserMarketModel;
use app\model\user\UserMemberModel;
use app\model\user\UserModel;
use app\model\user\UserSmsCodeModel;
use app\model\user\UserSuggestModel;
use think\Request;

/**
 * 推广员 BD
 * User
 * Class Market
 * @package app\controller
 */
class Market extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new UserModel();
    }

    /**
     * 申请成为推广员
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onMarket(Request $request)
    {
        $res['code'] = 0;
        $res['msg'] = "提交失败";
        $explain = $request->param('explain');
        $phone = $request->param('phone');
        $wechat = $request->param('wechat');
        if (!Util::isMobileNumber($phone)) {
            $res['code'] = 0;
            $res['msg'] = "手机号码格式不正确";
            return self::returnMsg($res);
        }
        if (empty($explain) || empty($phone) || empty($wechat)) {
            $res['code'] = 0;
            $res['msg'] = "参数有误";
        } else {
            $ret = UserMarketModel::addMarket($this->currentUser['user_id'], $explain, $phone, $wechat, 1);
            if ($ret !== false) {
                $res['code'] = 1;
                $res['msg'] = "提交成功";
            }
        }
        return self::returnMsg($res);
    }

    /**
     * 获取申请成为推广员信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMarket()
    {
        $user_market_model = new UserMarketModel();
        $map = [
            'user_id' => $this->currentUser['user_id']
        ];
        $market = $user_market_model->where($map)->find();
        if (empty($market)) {
            $res['code'] = 0;
            $res['data'] = [];
            $res['msg'] = "请填写数据";
            return self::returnMsg($res);
        }
        $res['code'] = 1;
        $res['data'] = $market;
        $res['msg'] = "获取成功";
        return self::returnMsg($res);
    }

    /**
     * 获取待处理事项列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCheckList()
    {
        $business_model = new BusinessModel();
        $order_model = new OrderModel();
        //获取推广员需要审核的商家列表
        $business_list = $business_model->getCheckList($this->currentUser['user_id']);
        $data['business_list'] = $business_list;
        //获取推广员需要审核的退货列表
        $order_list = $order_model->getOutOrderMarket($this->currentUser['user_id']);
        $data['order_list'] = $order_list;
        $res['code'] = 1;
        $res['data'] = $data;
        $res['msg'] = "获取成功";
        return self::returnMsg($res);
    }

    /**
     * (BD)推广员订单列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderList(Request $request)
    {
        $type = $request->param('type');//1当日订单 2全部订单
        $time = $request->param('time');//截止时间 2022-06-24
        $order_model = new OrderModel();
        $order_list = $order_model->getListOrderMarket($this->currentUser['user_id'], $type, $time);
        $res['code'] = 1;
        $res['data'] = $order_list;
        $res['msg'] = "获取成功";
        return self::returnMsg($res);
    }


    public function getBusinessList(Request $request)
    {
        $keyword = $request->param('keyword');

    }
}
